package elitech.vietnam.weatherfashion.tabs.styler;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import elitech.vietnam.weatherfashion.MainActivity;
import elitech.vietnam.weatherfashion.R;
import elitech.vietnam.weatherfashion.WFApplication;
import elitech.vietnam.weatherfashion.config.Utils;
import elitech.vietnam.weatherfashion.config.GlobalValue;
import elitech.vietnam.weatherfashion.dialog.PicPickerDialog;
import elitech.vietnam.weatherfashion.dto.MemberDTO;
import elitech.vietnam.weatherfashion.tabs.AbstractFragment;
import elitech.vietnam.weatherfashion.widget.NotifyButton;

public class StylerFragment extends AbstractFragment implements View.OnClickListener {

	public static final String TAB_ALL_ID = "ALL";
	public static final String TAB_FRIENDS_ID = "FRIENDS";

	private FragmentTabHost mTabHost;

	private Button button_camera;
	private TextView mTextHeader;
	private NotifyButton button_notification;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.styler, null);
		setupView(view);

		mTabHost = (FragmentTabHost) view.findViewById(R.id.styler_tabhost);
		button_camera = (Button) view.findViewById(R.id.styler_button_camera);
		mTextHeader = (TextView) view.findViewById(R.id.styler_text_header);
		button_notification = (NotifyButton) view.findViewById(R.id.button_notification);
		
		((MainActivity) getActivity()).setCurrentNotifyButton(button_notification);
		
		mTextHeader.setText(Html.fromHtml(getString(R.string.stylertitle)));
		initTabs();

		MemberDTO m = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (m == null)
			button_notification.setVisibility(View.GONE);
		else 
			button_notification.setVisibility(View.VISIBLE);
		//button_notification.setOnClickListener(this);

		button_camera.setOnClickListener(this);

		return view;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (getActivity().getIntent().getIntExtra("STYLER_FIRST_TAB", -1) > 0) {
			getActivity().getIntent().removeExtra("STYLER_FIRST_TAB");
			mTabHost.setCurrentTab(0);
		}
	}

	private void initTabs() {
		mTabHost.setup(getActivity(), getChildFragmentManager(), R.id.styler_realtabcontent);
		MemberDTO user = GlobalValue.getInstance(getActivity()).readUserInfo();
		if (user != null) {
			Bundle bundle = new Bundle();
			bundle.putString("page", TAB_ALL_ID);
			mTabHost.addTab(mTabHost.newTabSpec(TAB_ALL_ID).setIndicator(createTabView(getActivity(), R.drawable.allstyle_s, R.drawable.allstyle_d, getString(R.string.all_styler), true)),
					StylerChildFragment.class, bundle);
			bundle = new Bundle();
			bundle.putString("page", TAB_FRIENDS_ID);
			mTabHost.addTab(mTabHost.newTabSpec(TAB_FRIENDS_ID).setIndicator(createTabView(getActivity(), R.drawable.frstyle_s, R.drawable.frstyle_d, getString(R.string.followed_styler), true)),
					StylerChildFragment.class, bundle);
		} else {
			Bundle bundle = new Bundle();
			bundle.putString("page", TAB_ALL_ID);
			mTabHost.addTab(mTabHost.newTabSpec(TAB_ALL_ID).setIndicator(createTabView(getActivity(), R.drawable.frstyle_s, R.drawable.frstyle_d, getString(R.string.all_styler), false)),
					StylerChildFragment.class, bundle);
		}
	}

	private View createTabView(final Context context, int selected, int normal, String title, boolean isLoggedIn) {
		View view = LayoutInflater.from(context).inflate(R.layout.styler_tab, null);
		ImageView item_image = (ImageView) view.findViewById(R.id.styler_childtab_itemimage);
		TextView item_title = (TextView) view.findViewById(R.id.styler_childtab_itemtext);
		Drawable drawable = Utils.getTabDrawable(context, selected, normal);
		item_image.setImageDrawable(drawable);
		item_title.setText(title);
		if (!isLoggedIn)
			view.setVisibility(View.GONE);
		return view;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.styler_button_camera:
			MemberDTO user = GlobalValue.getInstance(getActivity()).readUserInfo();
			if (user == null) {
				Utils.openLogin(getActivity());
			} else
				new PicPickerDialog(getActivity(), StylerFragment.this).show();
			break;

		case R.id.button_notification:
			Log.w("Notification", "Num Count: " + WFApplication.getInstance().mListNoti.size());
			((MainActivity) getActivity()).mNotiWindow.showAsDropDown(button_notification);
			((MainActivity) getActivity()).mNotiWindow.notifyDataChanged();
			button_notification.setNumber(0);
			break;

		default:
			break;
		}

	}
}
